package com.example.testrecorder.view;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.example.testrecorder.R;

import java.util.List;

/**
 * @author Yingyong Lao
 * 创建时间 2021/5/29 10:55
 * @version 1.0
 */
public class MyPopupWindow extends PopupWindow {
    private Context context;
    private List<String> itemStrList;//子项文本列表
    private ListView listView;
    private OnItemClickCallBack clickCallBack;
    public int selectedPosition=-1;//记录被选中的子项的位置
    private TextView selectedTv;
    private View contentView;//代表整个PopupView

    //构造器
    public MyPopupWindow(Context context,List<String> itemStrList,OnItemClickCallBack clickCallBack){
        super();
        this.context=context;
        this.itemStrList=itemStrList;
        this.clickCallBack=clickCallBack;
        initView();//初始化MyPopupWindow的视图
        initPopupWindow();
    }

    private void initPopupWindow() {
        WindowManager windManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics metrics = new DisplayMetrics();
        windManager.getDefaultDisplay().getMetrics(metrics);//从默认显示器中获取显示参数保存到DisplayMetrics对象中
        float density = metrics.density;//像素密度
        int height = (int)(40*itemStrList.size()*density+0.5f);//将dp值转换为px，一个子项的高度是40，根据子项的个数决定总高度
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        this.setHeight(height);
        this.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#80000000")));
        this.setTouchable(true);
        this.setFocusable(true);
        this.setOutsideTouchable(true);
        this.setFocusable(false);
        this.setClippingEnabled(false);
        this.setContentView(contentView);
    }


    private void initView() {
        contentView = LayoutInflater.from(context).inflate(R.layout.my_popup_window_view, null);
        listView=contentView.findViewById(R.id.lv);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            }
        });
        contentView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();//关闭MyPopupWindow
            }
        });
        listView.setAdapter(new MyListAtapter());

    }


    public class MyListAtapter extends BaseAdapter{

        @Override
        public int getCount() {
            return itemStrList.size();
        }

        @Override
        public Object getItem(int position) {
            return itemStrList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if(convertView==null){
                convertView=LayoutInflater.from(context).inflate(R.layout.popup_list_item,parent,false);
            }
            TextView textView=convertView.findViewById(R.id.tv);
            textView.setText(itemStrList.get(position));
            if(selectedPosition==position){
                textView.setBackgroundColor(Color.parseColor("#FF5185CA"));//设置子项文本的背景颜色
                textView.setTextColor(Color.parseColor("#FFFFFFFF"));//设置文字颜色为白色
            }else {
                textView.setBackgroundColor(Color.parseColor("#FFFFFFFF"));//设置子项文本的背景颜色
                textView.setTextColor(Color.parseColor("#FF666666"));//设置文字颜色为白色
            }
            textView.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    if(selectedTv!=null){
                        selectedTv.setBackgroundColor(Color.parseColor("#FFFFFFFF"));
                        selectedTv.setTextColor(Color.parseColor("#FF666666"));
                    }
                    selectedTv=textView;
                    textView.setBackgroundColor(Color.parseColor("#FF5185CA"));//设置子项文本的背景颜色
                    textView.setTextColor(Color.parseColor("#FFFFFFFF"));//设置文字颜色为白色
                    return false;
                }
            });
            textView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    selectedPosition=position;
                    clickCallBack.click(itemStrList.get(position),position);
                }
            });
            return convertView;
        }
    }


    /**
     * 子项被点击的回调接口
     */
    public interface OnItemClickCallBack{
        public  void click(String itemStr,int itemPosition);
    }
}
